<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%zs_authtask}}".
 *
 * @property integer $task_id
 * @property string $task_name
 * @property integer $task_cat_id
 * @property string $description
 */
class Task extends \yii\db\ActiveRecord
{
    public $cat_arr = [
        1 => '角色',
        2 => '用户',
        3 => '任务',
    ];

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%zs_task}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['task_name'], 'required'],
            [['task_cat_id'], 'integer'],
            [['task_name'], 'string', 'max' => 64],
            [['description'], 'string', 'max' => 200],
            [['task_name'], 'unique']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'task_id' => 'Task ID',
            'task_name' => '任务名称',
            'task_cat_id' => '任务分类',
            'description' => '任务描述',
        ];
    }

    /**
     * 获取所有任务的数组，并专程task_id为键
     * @return array
     */
    public function getAllTaskToArray()
    {
        $result = $this->find()->asArray()->all();
        if(empty($result)){
            return [];
        }
        $tasks = [];
        foreach($result as $task){
            $tasks[$task['task_id']] = $task['task_name'];
        }
        return $tasks;
    }
}
